package com.tanhua.manager.service;

import com.tanhua.domain.db.UserInfo;
import com.tanhua.domain.mongo.Comment;
import com.tanhua.domain.mongo.Movement;
import com.tanhua.domain.mongo.Video;
import com.tanhua.domain.vo.CommentVo;
import com.tanhua.domain.vo.MovementsVo;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.VideoVo;
import com.tanhua.dubbo.api.db.UserInfoApi;
import com.tanhua.dubbo.api.mongo.CommentApi;
import com.tanhua.dubbo.api.mongo.MovementApi;
import com.tanhua.dubbo.api.mongo.VideoApi;
import org.apache.commons.lang3.StringUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ManageService {

    @Reference
    private UserInfoApi userInfoApi;

    public ResponseEntity queryUsers(Integer page, Integer pagesize, Long id, String nickname, String city) {

       PageResult pageResult = userInfoApi.queryUsers(page,pagesize,id,nickname,city);


        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity queryUsersById(Long userId) {

        UserInfo userInfo = userInfoApi.findById(userId);

        return ResponseEntity.ok(userInfo);
    }

    @Reference
    private VideoApi videoApi;

    public ResponseEntity queryVideos(Integer page, Integer pagesize, Long uid) {
//        查mongo  查mysql
        PageResult pageResult = videoApi.queryVideoListByUserId(page,pagesize,uid);
        List<Video> videoList = (List<Video>) pageResult.getItems();


        List<VideoVo> list = new ArrayList<>();
//        查mysql
        for (Video video : videoList) {
            UserInfo userInfo = userInfoApi.findById(video.getUserId());
            if(userInfo==null){
                continue;
            }
            VideoVo videoVo = new VideoVo();
            videoVo.setId(video.getId().toString());
            videoVo.setUserId(userInfo.getId());
            videoVo.setAvatar(userInfo.getAvatar());
            videoVo.setNickname(userInfo.getNickname());
            videoVo.setCover(video.getPicUrl()); //小视频封面
            videoVo.setVideoUrl(video.getVideoUrl());
            videoVo.setSignature(video.getText());
            videoVo.setLikeCount(video.getLikeCount());

            videoVo.setCommentCount(video.getCommentCount());

            list.add(videoVo);
        }
        pageResult.setItems(list);


        return ResponseEntity.ok(pageResult)  ;


    }

    @Reference
    private MovementApi movementApi;
    public ResponseEntity queryMessages(Integer page, Integer pagesize, Long uid, Integer state) {

        PageResult pageResult =  movementApi.queryUserMovements(uid,page,pagesize,state);

        List<Movement> items = (List<Movement>) pageResult.getItems();

        List<MovementsVo> list = new ArrayList<>();
        for (Movement movement : items) {
            UserInfo userInfo = userInfoApi.findById(movement.getUserId());
//            UserInfo+Movement---->MovementsVo
            MovementsVo movementsVo = movementAndUserInfoToMovementVo(movement, userInfo);

            list.add(movementsVo);
        }
        pageResult.setItems(list);

        return ResponseEntity.ok(pageResult);
    }

    public MovementsVo movementAndUserInfoToMovementVo(Movement movement, UserInfo userInfo) {
        MovementsVo movementsVo = new MovementsVo();
        BeanUtils.copyProperties(userInfo, movementsVo); //拷贝后发现userId和tags无法赋值
        movementsVo.setUserId(userInfo.getId());
        if (StringUtils.isNotBlank(userInfo.getTags())) {
            movementsVo.setTags(userInfo.getTags().split(","));
        }
        BeanUtils.copyProperties(movement, movementsVo);//发现有4个需要手动赋值
        movementsVo.setId(movement.getId().toString()); //一定要注意这个不要少！！！！！！
        movementsVo.setImageContent(movement.getMedias().toArray(new String[]{}));
        movementsVo.setDistance("500米以内");

//        这里的时间有一个坑 yapi上要一个long但是这里应该给一个 yyyy-MM-dd HH:mm 格式
        movementsVo.setCreateDate(new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new Date(movement.getCreated())));

        return movementsVo;
    }

    public ResponseEntity queryMessagesDetail(String movementId) {
        Movement movement = movementApi.queryMovementById(movementId);
        UserInfo userInfo = userInfoApi.findById(movement.getUserId());

        MovementsVo movementsVo = movementAndUserInfoToMovementVo(movement, userInfo);
        return ResponseEntity.ok(movementsVo);

    }

    @Reference
    private CommentApi commentApi;

    public ResponseEntity queryComments(Integer page, Integer pagesize, String movementId) {
        //        Mongo
        PageResult pageResult = commentApi.queryComments(movementId,page,pagesize);
        List<Comment> commentList = (List<Comment>) pageResult.getItems();
        List<CommentVo> list = new ArrayList<>();
//        mysql

        for (Comment comment : commentList) {
            UserInfo userInfo = userInfoApi.findById(comment.getUserId());
            CommentVo commentVo = new CommentVo();
            commentVo.setId(comment.getId().toString()); //评论
            commentVo.setAvatar(userInfo.getAvatar());
            commentVo.setNickname(userInfo.getNickname());
            commentVo.setContent(comment.getContent());
            commentVo.setCreateDate(new SimpleDateFormat("HH:mm").format( new Date(comment.getCreated()))); //创建时间  HH:mm
            commentVo.setLikeCount(comment.getLikeCount()); //这个评论收到的点赞数

            list.add(commentVo);
        }
        pageResult.setItems(list);

        return ResponseEntity.ok(pageResult);



    }
}
